package limeirafol.gui.Cadastro;

import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Random;
import javax.swing.AbstractCellEditor;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreeSelectionModel;
import limeirafol.remote.cadastro.PermissaoVO;
import lbframework.classe.Util;
import lbframework.bean.internalFrame.LBInternalFrame;
import lbframework.bean.mdiFrame.LBMdiFrame;
import lbframework.bean.tree.LBItemNode;
import lbframework.bean.tree.LBTreeModel;
import limeirafol.Global;
import limeirafol.bean.BeanCadastro;
import limeirafol.remote.Formulario;
import limeirafol.remote.TipoOperacao;
import limeirafol.remote.TipoOperacaoEstoque;
import limeirafol.remote.UsuarioVO;
import limeirafol.remote.cadastro.Situacao;

public class UsuarioCadastroGUI extends LBInternalFrame {

    private BeanCadastro beanCadastro = new BeanCadastro();
    private UsuarioVO oUsuario = null;

    public UsuarioCadastroGUI(LBMdiFrame i_mdiFrame) throws Exception {
        super(i_mdiFrame);
        initComponents();

        centralizarForm();

        cboSituacao.setTabela("situacaocadastro");
        cboSituacao.carregar();

    }

    public void carregarUsuario(int i_id) throws Exception {
        oUsuario = beanCadastro.carregarUsuario(i_id);

        txtCodigo.setText(Util.formatNumber(oUsuario.id, 6));
        txtNome.setText(oUsuario.nome);
        txtLogin.setText(oUsuario.login);
        txtSenha.setText("******");
        cboSituacao.setId(oUsuario.idSituacaoCadastro);


        exibirPermissao();

        getEstadoTelaInicial();

        if (oUsuario.idSituacaoCadastro == Situacao.ATIVO.getId()) {
            cboSituacao.setEnabled(false);
        }

        txtCodigo.requestFocus();
    }

    private void exibirPermissao() throws Exception {
        LBTreeModel oTreeModel = new LBTreeModel();
        oTreeModel.folderRoot.setDescricao("PERMISSAO");

        ItemNodeVO nodePrograma = null;
        ItemNodeVO nodeModulo = null;
        ItemNodeVO nodeFormulario = null;

        int idPrograma = -1;
        int idModulo = -1;
        int idFormulario = -1;

        for (PermissaoVO oPermissao : oUsuario.vPermissao) {
            if (oPermissao.idPrograma != idPrograma) {
                nodePrograma = new ItemNodeVO(oPermissao.programa, 0);
                nodePrograma.idPrograma = oPermissao.idPrograma;

                oTreeModel.folderRoot.addChild(nodePrograma);

                idPrograma = -1;
            }

            if (oPermissao.idModulo != idModulo) {
                nodeModulo = new ItemNodeVO(oPermissao.modulo, 0);
                nodeModulo.idModulo = oPermissao.idModulo;

                nodePrograma.addChild(nodeModulo);

                idFormulario = -1;
            }

            if (oPermissao.idFormulario != idFormulario) {
                nodeFormulario = new ItemNodeVO(oPermissao.formulario, 0);
                nodeFormulario.idFormulario = oPermissao.idFormulario;

                nodeModulo.addChild(nodeFormulario);
            }

            ItemNodeVO nodeOperacao = new ItemNodeVO(oPermissao.tipoOperacao, oPermissao.selecionado ? 1 : 0);
            nodeOperacao.idFormulario = oPermissao.idFormulario;
            nodeOperacao.idTipoOperacao = oPermissao.idTipoOperacao;

            nodeFormulario.addChild(nodeOperacao);

            idPrograma = oPermissao.idPrograma;
            idModulo = oPermissao.idModulo;
            idFormulario = oPermissao.idFormulario;
        }

        oTreeModel.verificar(oTreeModel.folderRoot);

        trePermissao.setModel(oTreeModel);
        trePermissao.setCellRenderer(new LBCheckBoxCellRenderer());
        trePermissao.setCellEditor(new LBCheckBoxNodeEditor());

        trePermissao.setEditable(true);
        trePermissao.setShowsRootHandles(true);
        trePermissao.setRootVisible(true);

        trePermissao.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    }

    @Override
    public void incluir() throws Exception {
        Global.verificarPermissao(Formulario.CADASTRO_USUARIO, TipoOperacao.INCLUIR);
        verificarAlteracaoTela();

        txtCodigo.setText("");
        txtSenha.setText("");
        txtLogin.setText("");
        txtNome.setText("");
        cboSituacao.setId(Situacao.ATIVO.getId());
        cboSituacao.setEnabled(false);


        gerarSenha();

        oUsuario = new UsuarioVO();

        oUsuario.vPermissao = beanCadastro.carregarPermissao();
        exibirPermissao();

        txtNome.requestFocus();

        getEstadoTelaInicial();
    }

    @Override
    public void copiar() throws Exception {
        Global.verificarPermissao(Formulario.CADASTRO_USUARIO, TipoOperacao.INCLUIR);
        verificarAlteracaoTela();

        oUsuario.id = 0;

        txtCodigo.setText("");
        txtNome.setText("");
        txtLogin.setText("");

        gerarSenha();

        txtNome.requestFocus();

        getEstadoTelaInicial();
    }

    private void verificarPermissao(LBItemNode i_node) throws Exception {
        for (int i = 0; i < i_node.getChildCount(); i++) {
            ItemNodeVO oNode = (ItemNodeVO) i_node.getChild(i);
            verificarPermissao(oNode);

            for (PermissaoVO oPermissao : oUsuario.vPermissao) {
                if ((oNode.idFormulario > 0) && (oPermissao.idFormulario == oNode.idFormulario) && (oPermissao.idTipoOperacao == oNode.idTipoOperacao)) {

                    oPermissao.selecionado = oNode.selecionado == 1 ? true : false;
                    break;
                }
            }
        }
    }

    @Override
    public void salvar() throws Exception {
        Util.validarCampoTela(this.getCampoObrigatorio());

        oUsuario.id = txtCodigo.getInt();
        oUsuario.nome = txtNome.getText();
        oUsuario.idSituacaoCadastro = cboSituacao.getId();
        oUsuario.login = txtLogin.getText();

        if (txtSenha.getEchoChar() == 0) {
            oUsuario.senha = Util.getMd5(txtSenha.getText());
        }

        verificarPermissao(((LBTreeModel) trePermissao.getModel()).folderRoot);

        beanCadastro.salvarUsuario(oUsuario);

        txtCodigo.setText(Util.formatNumber(oUsuario.id, 6));

        if (cboSituacao.getId() == Situacao.ATIVO.getId()) {
            cboSituacao.setEnabled(false);
        }

        Util.exibirMensagem(Util.MSG_SALVO_SUCESSO, getTitle());

        getEstadoTelaInicial();
    }

    public void gerarSenha() throws Exception {
        Random rnd = new Random();

        String senha = "";

        for (int i = 0; i < 6; i++) {
            int letra = rnd.nextInt(26) + 65;
            int numero = rnd.nextInt(10);

            if (rnd.nextInt(2) == 0) {
                senha += String.valueOf((char) letra);
            } else {
                senha += String.valueOf(numero);
            }
        }

        txtSenha.setEchoChar(((char) 0));
        txtSenha.setText(senha);
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        vRPanel2 = new lbframework.bean.panel.LBPanel();
        btnSair = new lbframework.bean.button.LBButton();
        btnSalvar = new lbframework.bean.button.LBButton();
        vRToolBarPadrao1 = new lbframework.bean.toolBarPadrao.LBToolBarPadrao(this);
        vRTabbedPane1 = new lbframework.bean.tabbedPane.LBTabbedPane();
        vRPanel1 = new lbframework.bean.panel.LBPanel();
        Código = new lbframework.bean.label.LBLabel();
        vRLabel1 = new lbframework.bean.label.LBLabel();
        vRLabel2 = new lbframework.bean.label.LBLabel();
        txtLogin = new lbframework.bean.textField.LBTextField();
        vRLabel3 = new lbframework.bean.label.LBLabel();
        cboSituacao = new lbframework.bean.comboBox.LBComboBox();
        vRLabel7 = new lbframework.bean.label.LBLabel();
        txtSenha = new lbframework.bean.passwordField.LBPasswordField();
        txtCodigo = new lbframework.bean.textField.LBTextField();
        txtNome = new lbframework.bean.textField.LBTextField();
        btnSenha = new lbframework.bean.button.LBButton();
        vRPanel3 = new lbframework.bean.panel.LBPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        trePermissao = new lbframework.bean.tree.LBTree();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Cadastro de Usuário");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosed(evt);
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        btnSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/sair.png"))); // NOI18N
        btnSair.setText("Sair");
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });

        btnSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/salvar.png"))); // NOI18N
        btnSalvar.setText("Salvar");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout vRPanel2Layout = new javax.swing.GroupLayout(vRPanel2);
        vRPanel2.setLayout(vRPanel2Layout);
        vRPanel2Layout.setHorizontalGroup(
            vRPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, vRPanel2Layout.createSequentialGroup()
                .addContainerGap(329, Short.MAX_VALUE)
                .addComponent(btnSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSair, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        vRPanel2Layout.setVerticalGroup(
            vRPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(vRPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btnSair, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btnSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        vRToolBarPadrao1.setRollover(true);
        vRToolBarPadrao1.setVisibleCopiar(true);
        vRToolBarPadrao1.setVisibleIncluir(true);
        vRToolBarPadrao1.setVisibleSalvar(true);

        Código.setText("Código");

        vRLabel1.setText("Nome");

        vRLabel2.setText("Login");

        txtLogin.setColumns(12);
        txtLogin.setObrigatorio(true);

        vRLabel3.setText("Senha");

        cboSituacao.setObrigatorio(true);

        vRLabel7.setText("Situação");

        txtSenha.setColumns(6);
        txtSenha.setEnabled(false);
        txtSenha.setObrigatorio(true);

        txtCodigo.setEnabled(false);

        txtNome.setColumns(30);
        txtNome.setObrigatorio(true);

        btnSenha.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/senha.png"))); // NOI18N
        btnSenha.setToolTipText("Gerar senha");
        btnSenha.setFocusable(false);
        btnSenha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSenhaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout vRPanel1Layout = new javax.swing.GroupLayout(vRPanel1);
        vRPanel1.setLayout(vRPanel1Layout);
        vRPanel1Layout.setHorizontalGroup(
            vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(vRPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(vRPanel1Layout.createSequentialGroup()
                        .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(Código, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(vRLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, 259, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(vRLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(vRPanel1Layout.createSequentialGroup()
                        .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(vRLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(258, 258, 258)
                        .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cboSituacao, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(vRLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(293, 293, 293))
        );
        vRPanel1Layout.setVerticalGroup(
            vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(vRPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Código, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(vRLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(vRLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(vRPanel1Layout.createSequentialGroup()
                        .addComponent(vRLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(vRPanel1Layout.createSequentialGroup()
                        .addComponent(vRLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cboSituacao, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(257, Short.MAX_VALUE))
        );

        vRTabbedPane1.addTab("Dados Usuário", vRPanel1);

        trePermissao.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                trePermissaoMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(trePermissao);

        javax.swing.GroupLayout vRPanel3Layout = new javax.swing.GroupLayout(vRPanel3);
        vRPanel3.setLayout(vRPanel3Layout);
        vRPanel3Layout.setHorizontalGroup(
            vRPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(vRPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 464, Short.MAX_VALUE)
                .addContainerGap())
        );
        vRPanel3Layout.setVerticalGroup(
            vRPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(vRPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 332, Short.MAX_VALUE)
                .addContainerGap())
        );

        vRTabbedPane1.addTab("Permissão", vRPanel3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(vRToolBarPadrao1, javax.swing.GroupLayout.DEFAULT_SIZE, 501, Short.MAX_VALUE)
                    .addComponent(vRPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(vRTabbedPane1, 0, 0, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(vRToolBarPadrao1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(vRTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 395, Short.MAX_VALUE)
                .addGap(3, 3, 3)
                .addComponent(vRPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        try {
            verificarAlteracaoTela();
            sair();

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, getTitle());
        }
    }//GEN-LAST:event_btnSairActionPerformed

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
        try {
            salvar();

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, getTitle());
        }
    }//GEN-LAST:event_btnSalvarActionPerformed

    private void formInternalFrameClosed(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosed
    }//GEN-LAST:event_formInternalFrameClosed

    private void trePermissaoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_trePermissaoMouseClicked
}//GEN-LAST:event_trePermissaoMouseClicked

    private void btnSenhaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSenhaActionPerformed
        try {
            gerarSenha();

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, getTitle());
        }
}//GEN-LAST:event_btnSenhaActionPerformed

    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
        try {
            verificarAlteracaoTela();
            sair();

        } catch (Exception e) {
        }
    }//GEN-LAST:event_formInternalFrameClosing

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private lbframework.bean.label.LBLabel Código;
    private lbframework.bean.button.LBButton btnSair;
    private lbframework.bean.button.LBButton btnSalvar;
    private lbframework.bean.button.LBButton btnSenha;
    private lbframework.bean.comboBox.LBComboBox cboSituacao;
    private javax.swing.JScrollPane jScrollPane1;
    private lbframework.bean.tree.LBTree trePermissao;
    private lbframework.bean.textField.LBTextField txtCodigo;
    private lbframework.bean.textField.LBTextField txtLogin;
    private lbframework.bean.textField.LBTextField txtNome;
    private lbframework.bean.passwordField.LBPasswordField txtSenha;
    private lbframework.bean.label.LBLabel vRLabel1;
    private lbframework.bean.label.LBLabel vRLabel2;
    private lbframework.bean.label.LBLabel vRLabel3;
    private lbframework.bean.label.LBLabel vRLabel7;
    private lbframework.bean.panel.LBPanel vRPanel1;
    private lbframework.bean.panel.LBPanel vRPanel2;
    private lbframework.bean.panel.LBPanel vRPanel3;
    private lbframework.bean.tabbedPane.LBTabbedPane vRTabbedPane1;
    private lbframework.bean.toolBarPadrao.LBToolBarPadrao vRToolBarPadrao1;
    // End of variables declaration//GEN-END:variables

    class ItemNodeVO extends LBItemNode {

        public int idTipoOperacao = 0;
        public int idFormulario = 0;
        public int idModulo = 0;
        public int idPrograma = 0;

        public ItemNodeVO(String i_descricao, int i_selecionado) {
            super(i_descricao, i_selecionado);
        }
    }

    class LBCheckBoxCellRenderer implements TreeCellRenderer {

        private JCheckBox checkbox = new JCheckBox();

        public LBCheckBoxCellRenderer() {
        }

        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded,
                boolean leaf, int row, boolean hasFocus) {

            if (value instanceof ItemNodeVO) {
                ItemNodeVO folder = (ItemNodeVO) value;

                checkbox.setText(folder.toString());
                checkbox.setSelected(folder.getSelecionado() > 0 ? true : false);

                if (folder.idTipoOperacao > 0) {
                    if (folder.idTipoOperacao == TipoOperacaoEstoque.CONSULTAR.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/consultar.png")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/consultarchecked.png")));

                    } else if (folder.idTipoOperacao == TipoOperacaoEstoque.ALTERAR.getId() || folder.idTipoOperacao == TipoOperacaoEstoque.ALTERAR_IMPOSTO.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/alterar.png")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/alterarchecked.png")));

                    } else if (folder.idTipoOperacao == TipoOperacaoEstoque.INCLUIR.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/incluir.png")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/incluirchecked.png")));

                    } else if (folder.idTipoOperacao == TipoOperacaoEstoque.EXCLUIR.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/excluir.png")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/excluirchecked.png")));

                    } else if (folder.idTipoOperacao == TipoOperacaoEstoque.CANCELAR.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/cancelar.png")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/cancelarchecked.png")));

                    } else if (folder.idTipoOperacao == TipoOperacaoEstoque.BAIXAR.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/baixar.png")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/baixarchecked.png")));

                    } else if (folder.idTipoOperacao == TipoOperacaoEstoque.INICIAR.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/iniciar.png")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/iniciarchecked.png")));

                    } else if (folder.idTipoOperacao == TipoOperacaoEstoque.ENTRADA_ESTOQUE.getId() || folder.idTipoOperacao == TipoOperacaoEstoque.SAIDA_ESTOQUE.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/alterar.png")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/alterarchecked.png")));

                    } else if (folder.idTipoOperacao == TipoOperacaoEstoque.EXPORTAR.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/exportar.png")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/exportarchecked.png")));

                    } else if (folder.idTipoOperacao == TipoOperacaoEstoque.ESTORNAR.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/estornar.png")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/estornarchecked.png")));

                    } else if (folder.idTipoOperacao == TipoOperacaoEstoque.PARAMETRIZAR.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/parametrizar.png")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/parametrizarchecked.png")));

                    } else if (folder.idTipoOperacao == TipoOperacaoEstoque.IMPORTAR.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/importar.png")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/importarchecked.png")));

                    } else if (folder.idTipoOperacao == TipoOperacaoEstoque.ENVIAR.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/enviar.PNG")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/enviarchecked.PNG")));

                    } else if (folder.idTipoOperacao == TipoOperacaoEstoque.FINALIZAR.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/finalizar.png")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/finalizarchecked.png")));

                    } else if (folder.idTipoOperacao == TipoOperacaoEstoque.IMPRIMIR.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/imprimir.PNG")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/imprimirchecked.PNG")));

                    } else if (folder.idTipoOperacao == TipoOperacaoEstoque.BLOQUEAR.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/bloqueado.png")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/bloqueadochecked.png")));

                    } else if (folder.idTipoOperacao == TipoOperacaoEstoque.CREDITO_ALIMENTACAO.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/credito_alimentacao.png")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/credito_alimentacaochecked.png")));

                    } else if (folder.idTipoOperacao == TipoOperacaoEstoque.GERAR.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/gerar.png")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/gerarchecked.png")));

                    } else if (folder.idTipoOperacao == TipoOperacaoEstoque.CONFIGURAR.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/configurar.png")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/configurarchecked.png")));

                    } else if (folder.idTipoOperacao == TipoOperacaoEstoque.CONFERIR.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/conferir.png")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/conferirchecked.png")));

                    } else if (folder.idTipoOperacao == TipoOperacaoEstoque.ENVIAR_CARGA.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/carga.png")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/cargachecked.png")));

                    } else if (folder.idTipoOperacao == TipoOperacaoEstoque.TRANSMITIR.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/transmitir.png")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/transmitirchecked.png")));

                    } else if (folder.idTipoOperacao == TipoOperacaoEstoque.INUTILIZAR.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/inutilizar.png")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/inutilizarchecked.png")));

                    } else if (folder.idTipoOperacao == TipoOperacaoEstoque.CONSISTIR.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/consistir.png")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/consistirchecked.png")));

                    } else if (folder.idTipoOperacao == TipoOperacaoEstoque.IMPORTAR_COM_DIVERGENCIA.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/importarDivergente.png")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/importarDivergenteChecked.png")));

                    } else if (folder.idTipoOperacao == TipoOperacaoEstoque.ENVIAR_MENSAGEM.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/enviarmensagem.png")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/enviarmensagemchecked.png")));

                    } else if (folder.idTipoOperacao == TipoOperacaoEstoque.LIBERAR.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/liberar.png")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/liberarchecked.png")));

                    } else if (folder.idTipoOperacao == TipoOperacaoEstoque.REPROCESSAR.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/reprocessar.png")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/reprocessarchecked.png")));

                    } else if (folder.idTipoOperacao == TipoOperacaoEstoque.AGRUPAR.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/agrupar.png")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/agruparchecked.png")));

                    } else if (folder.idTipoOperacao == TipoOperacaoEstoque.DESCONECTAR.getId()) {
                        checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/desconectar.png")));
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/desconectarchecked.png")));

                    } else {
                        checkbox.setIcon(null);
                        checkbox.setSelectedIcon(null);
                    }

                } else if (folder.idFormulario > 0) {
                    checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/icone2.png")));

                    if (folder.selecionado == 1) {
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/icone2checked.png")));
                    } else if (folder.selecionado == 2) {
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/icone2grayed.png")));
                    }
                } else if (folder.idModulo > 0) {
                    checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/icone1.png")));

                    if (folder.selecionado == 1) {
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/icone1checked.png")));
                    } else if (folder.selecionado == 2) {
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/icone1grayed.png")));
                    }
                } else if (folder.idPrograma >= 0) {
                    checkbox.setIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/icone3.png")));

                    if (folder.selecionado == 1) {
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/icone3checked.png")));
                    } else if (folder.selecionado == 2) {
                        checkbox.setSelectedIcon(new ImageIcon(getClass().getResource("/lbframework/img/checkBox/icone3grayed.png")));
                    }
                }

                checkbox.setForeground(UIManager.getColor("Tree.textForeground"));
                checkbox.setBackground(UIManager.getColor("Tree.textBackground"));

                checkbox.setEnabled(tree.isEnabled());

                return checkbox;

            } else {
                LBItemNode folder = (LBItemNode) value;

                checkbox.setText(folder.toString());
                checkbox.setSelected(folder.getSelecionado() > 0 ? true : false);

                checkbox.setIcon(UIManager.getIcon("FileView.hardDriveIcon"));
                checkbox.setSelectedIcon(UIManager.getIcon("FileView.hardDriveIcon"));

                checkbox.setForeground(UIManager.getColor("Tree.textForeground"));
                checkbox.setBackground(UIManager.getColor("Tree.textBackground"));

                return checkbox;
            }
        }
    }

    class LBCheckBoxNodeEditor extends AbstractCellEditor implements TreeCellEditor {

        private TreeCellRenderer renderer = new LBCheckBoxCellRenderer();
        private JCheckBox editor;
        private boolean firstCall = true;

        @Override
        public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded,
                boolean leaf, int row) {

            editor = (JCheckBox) renderer.getTreeCellRendererComponent(tree, value, isSelected, expanded, leaf, row, true);

            if (firstCall) {
                ItemListener itemListener = new ItemListener() {

                    @Override
                    public void itemStateChanged(ItemEvent itemEvent) {
                        fireEditingStopped();
                    }
                };
                editor.addItemListener(itemListener);
                firstCall = false;
            }

            return editor;
        }

        @Override
        public Object getCellEditorValue() {
            return editor.isSelected();
        }
    }
}
